package com.example.sharedpreferences1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.prefs.Preferences;

public class RememberPasswordDemoActivity extends AppCompatActivity {
    private SharedPreferences preferences;//key,walue键值方式
    private EditText editText_inputname;
    private EditText editText_inputpwd;
    private CheckBox checkBox_remember;
    private Button button_login;
    private Button button_cancle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.rpword);
        preferences=PreferenceManager.getDefaultSharedPreferences(this);

        editText_inputname=findViewById(R.id.editText_inputname);
        editText_inputpwd=findViewById(R.id.editText_inputpwd);
        checkBox_remember=findViewById(R.id.checkBox_remember);
        boolean isRemember=preferences.getBoolean("remember",false);
        if(isRemember){
            editText_inputname.setText(preferences.getString("username",""));
        editText_inputpwd.setText(preferences.getString("pwd",""));
        checkBox_remember.setChecked(true);
        }
    }
    public void setLoginBtn(View view){
    boolean remember=checkBox_remember.isChecked();
    SharedPreferences.Editor editor=preferences.edit();
    if(remember) {
        //
        String userName = editText_inputname.getText().toString();
        String psw = editText_inputpwd.getText().toString();
        editor.putString("username", userName);
        editor.putString("pwd", psw);
        editor.putBoolean("remember", true);
        editor.apply();
        Toast.makeText(this, "用户和密码已保存", Toast.LENGTH_SHORT).show();

    }else{
        editor.clear();
        editor.apply();
    }
        Intent intent = new Intent(RememberPasswordDemoActivity.this, MainActivity.class);
        startActivity(intent);
    }






}
